package com.zdb.utils;

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.Map;

public class ChatRequestBuilder {

    private JSONObject root;

    public ChatRequestBuilder(String model) {
        root = new JSONObject();
        root.put("model", model);
        root.put("messages", new JSONArray());
    }

    /**
     * 添加一条消息
     * @param role 角色，通常是 "user" 或 "assistant"
     * @param content 消息内容
     * @return 自身，方便链式调用
     */
    public ChatRequestBuilder addMessage(String role, String content) {
        JSONObject message = new JSONObject();
        message.put("role", role);
        message.put("content", content);
        root.getJSONArray("messages").put(message);
        return this;
    }

    /**
     * 也支持直接传入多条消息，比如List<Map<String, String>>
     */
    public ChatRequestBuilder addMessages(List<Map<String, String>> messages) {
        JSONArray array = root.getJSONArray("messages");
        for (Map<String, String> msg : messages) {
            JSONObject message = new JSONObject(msg);
            array.put(message);
        }
        return this;
    }

    public String build() {
        return root.toString();
    }

}
